import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

/// 授权返回
class PermissionsStatus {
  final bool enable;
  final String message;
  final String title;

  PermissionsStatus({required this.enable, required this.message, required this.title});
}

/// 动态授权
class PermissionUtils {
  /// 是否有相册权限
  static Future<PermissionsStatus> photos() async {
    var value = false;
    if (Platform.isIOS) {
      var statuses = await [Permission.photos].request();
      value = statuses[Permission.photos] == PermissionStatus.granted;
    }
    if (Platform.isAndroid) {
      DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
      if (androidInfo.version.sdkInt >= 33) {
        var statuses = await [Permission.photos].request();
        value = statuses[Permission.photos] == PermissionStatus.granted;
      } else {
        var statuses = await [Permission.storage].request();
        value = statuses[Permission.storage] == PermissionStatus.granted;
      }
    }
    return PermissionsStatus(
      enable: value,
      message: value ? '' : '无法访问相册，如果您希望使用相册，请先前往"设置"中打开相册${Platform.isAndroid ? '和存储' : ''}权限',
      title: '需要授权',
    );
  }

  /// 是否获取本地资源权限（图片、视频、音频等，内部根据不同平台、API版本做兼容）
  static Future<PermissionsStatus> assets() async {
    var value = false;
    final PermissionState ps = await PhotoManager.requestPermissionExtend();
    if (ps.isAuth) {
      value = true;
    }
    return PermissionsStatus(
      enable: value,
      message: value ? '' : '无法访问相机，如果您希望使用相机，请先前往"设置"中打开相机${Platform.isAndroid ? '和存储' : ''}权限',
      title: '需要授权',
    );
  }

  /// 是否有麦克风权限
  static Future<PermissionsStatus> microphone() async {
    var value = false;
    var statuses = await [Permission.microphone].request();
    value = statuses[Permission.microphone] == PermissionStatus.granted;
    return PermissionsStatus(
      enable: value,
      message: value ? '' : '无法访问麦克风，如果您希望使用麦克风，请先前往"设置"中打开麦克风权限',
      title: '需要授权',
    );
  }

  /// 是否有存储权限
  static Future<bool> storage() async {
    bool value = false;
    if (Platform.isAndroid) value = await Permission.storage.request().isGranted;
    if (Platform.isIOS) value = true;
    if (!value) {
      Fluttertoast.showToast(msg: '请先前往"设置"中打开存储权限');
      await Future.delayed(const Duration(seconds: 1));
      openSettings();
    }
    return value;
  }


  /// 是否有定位权限
  static Future<PermissionsStatus> location() async {
    var value = false;
    var statuses = await [Permission.location].request();
    value = statuses[Permission.location] == PermissionStatus.granted;
    return PermissionsStatus(
      enable: value,
      message: value ? '' : '无法访问定位，如果您希望使用定位，请先前往"设置"中打开定位权限',
      title: '需要授权',
    );
  }

  /// 打开系统设置
  static Future<bool> openSettings() async {
    return await openAppSettings();
  }

  ///为语音消息提前申请录音存储权限
  static Future<void> audio() async {
    if (Platform.isAndroid) {
      Permission permission = await storageForAndroidAudio();
      await [Permission.microphone, permission].request();
    }
    if (Platform.isIOS) {
      await [Permission.microphone].request();
    }
  }

  static Future<Permission> storageForAndroidAudio() async {
    if (Platform.isIOS) {
      return Permission.storage;
    }
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
    return androidInfo.version.sdkInt >= 33 ? Permission.audio : Permission.storage;
  }
}
